<?php
/**
 * Created by PhpStorm
 * User: Jhinwy
 * Date: 6/13/24
 * Time: 3:58 PM
 */


namespace app\common\command;

use app\common\model\media\Media;
use app\common\model\media\MediaCreative;
use app\common\model\media_account\MediaAccount;
use app\common\service\media\BdService;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\console\Command;

class PullBdCreative extends Command
{
    protected function configure()
    {
        $this->setName('pull_bd_creative')
            ->addOption('date', null, Option::VALUE_OPTIONAL, '拉取日期')
            ->setDescription('拉取百度素材创意');
    }


    /**
     * 拉取百度视频素材
     *
     * @param Input $input
     * @param Output $output
     * @return bool|int|null
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function execute(Input $input, Output $output)
    {
        // 获取需要拉取报表的媒体账户
        $accounts = MediaAccount::with('link')
            ->where([
                ['media_id', '=', Media::SOURCE_BD],
                ['status', '=', 1],
                ['promotion_link_id', '<>', 0]
            ])->select();

        if ($accounts->isEmpty()) {

            return false;
        }
        $output->writeln('开始拉取百度素材，共计：' . $accounts->count() . '条');

        // 拉取时间
        if ($input->hasOption('date')) {
            $pullDate = $input->getOption('date');
        } else {
            $pullDate = date('Y-m-d', time());
        }

        foreach ($accounts as $account) {
            $service = new BdService($account->media_auth_id);
            if (!$link = $account->link ?? []) {
                continue;
            }
            $result = $service->getVideoReport(
                $account->auth_advertiser_id, $account->advertiser_id, $account->advertiser_name, $pullDate
            );
            if (!$result) {
                continue;
            }

            foreach ($result as $item) {
                if (!$item) {
                    continue;
                }
                $mc = MediaCreative::where('advertiser_id', $account->advertiser_id)
                    ->where('media_id', Media::SOURCE_BD)
                    ->where('promotion_link_id', $link->id)
                    ->where('count_date', $pullDate)
                    ->where('package_name', $item['package_name'])
                    ->findOrEmpty();

                if ($mc->isEmpty()) {
                    $mc                    = new MediaCreative();
                    $mc->media_id          = Media::SOURCE_BD;
                    $mc->advertiser_id     = $account->advertiser_id;
                    $mc->promotion_link_id = $link->id;
                    $mc->count_date        = $pullDate;
                }

                $mc->cover_image_urls = $item['cover_image_urls'];
                $mc->package_name     = $item['package_name'];
                $mc->charge           = $item['charge'];
                $mc->show             = $item['show'];
                $mc->photo_click      = $item['photo_click'];
                $mc->play_3s_ratio    = $item['play_3s_ratio'];
                $mc->video_urls       = $item['video_urls'];

                $mc->save();

            }
        }

        $output->writeln('拉取百度视频素材完成');
    }
}
